package chapter14;

/**
 * @author : DuJiabao
 * @Date : 2021/2/25 15:10
 * @Project : thinking_in_java
 * @File : null.java
 * @Desc :
 */
interface Interface {
  void doSomething();

  void SomethingElse(String arg);
}

/**
 * 接口简单的实现，真实的对象
 */
class RealObject implements Interface {

  @Override
  public void doSomething() {
    System.out.println("doSomething");
  }

  @Override
  public void SomethingElse(String arg) {
    System.out.println("SomethingElse " + arg);
  }
}

/**
 * 可以实现，在原来对象的基础上，额外增加一些需要的功能
 */
class SimpleProxy implements Interface {
  private Interface proxied;

  /**
   * 传进一个代理对象
   *
   * @param proxied
   */
  public SimpleProxy(Interface proxied) {
    this.proxied = proxied;
  }

  @Override
  public void doSomething() {
    System.out.println("SimpleProxy doSomething ");
    proxied.doSomething();
  }

  @Override
  public void SomethingElse(String arg) {
    System.out.println("SimpleProxy SomethingElse " + arg);
    proxied.SomethingElse(arg);
  }
}


public class SimpleProxyDemo {
  public static void consumer(Interface iface) {
    iface.doSomething();
    iface.SomethingElse("bonobo");
  }

  public static void main(String[] args) {
    consumer(new RealObject());
    consumer(new SimpleProxy(new RealObject()));
  }
}
